﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace _180FrameWork.Common
{
    public class RegistHelper
    {
        /// <summary>
        /// 创建项
        /// </summary>
        /// <param name="regKey"></param>
        /// <param name="subKey"></param>
        public static int mCreateSubKey(RegistryKey regKey, string subKey)
        {
            int res = 0;
            try
            {
                RegistryKey key = regKey;
                RegistryKey s = key.OpenSubKey(subKey, true);
                if (s == null)
                {
                    s = key.CreateSubKey(subKey);
                }
                key.Close();
                s.Close();
                res = 1;
            }
            catch (Exception ex)
            {
                res = 0;
            }
            return res;
        }
        /// <summary>
        /// 判断是否存在项
        /// </summary>
        /// <param name="regKey"></param>
        /// <param name="subKey"></param>
        /// <param name="itemName"></param>
        /// <returns></returns>
        public static bool isExistItem(RegistryKey regKey, string subKey , string itemName)
        {

            bool res = false;
            try
            {

                RegistryKey key = regKey;

                RegistryKey s = key.OpenSubKey(subKey , true);

                string[] subkeyNames;

                subkeyNames = s.GetSubKeyNames();

                foreach (var el in subkeyNames)
                {
                    if (el == itemName)
                    {
                        res = true;
                    }
                }

                key.Close();

                s.Close();

            }
            catch (Exception ex)
            {
                res = false;
            }

            return res;
        
        
        }

        /// <summary>
        /// 是否存在某个项目
        /// </summary>
        /// <param name="regKey"></param>
        /// <param name="subKey"></param>
        /// <returns></returns>
        public static bool isExistKeyValue(RegistryKey regKey, string subKey, string keyName)
        {
            bool res = false;
            try
            {
                RegistryKey key = regKey;
                RegistryKey s = key.OpenSubKey(subKey, true);

                string[] subkeyNames;

                subkeyNames = s.GetValueNames();

                foreach (var el in subkeyNames)
                {
                    if (el == keyName) {
                        res = true;
                    }
                }

                key.Close();
                s.Close();
                
            }
            catch (Exception ex)
            {
                res = false;
            }
            
            return res;
        }


        /// <summary>
        /// 写值
        /// </summary>
        /// <param name="regKey"></param>
        /// <param name="subKey"></param>
        /// <param name="path"></param>
        public static int mSetValue(RegistryKey regKey, string subKey, string text, string value, RegistryValueKind valueKind)
        {
            int res = 0;
            try
            {
                RegistryKey key = regKey;
                RegistryKey path = key.OpenSubKey(subKey, true); //该键值须存在
                //"name"是该键值的name，相当于一个别名，可自行设置
                path.SetValue(text, value, valueKind);
                key.Close();
                path.Close();
                res = 1;
            }
            catch (Exception)
            {
                res = 0;
            }
            return res;
        }
        /// <summary>
        /// 读值
        /// </summary>
        /// <param name="regKey"></param>
        /// <param name="subKey"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string mGetValue(RegistryKey regKey, string subKey, string key)
        {
            string value = "";

            RegistryKey mkey = regKey;
            RegistryKey s = mkey.OpenSubKey(subKey, true);

            if (s != null)
            {
                Object res = s.GetValue(key);
                if (res != null)
                {
                    value = res.ToString();
                }
            }
            mkey.Close();
            s.Close();

            return value;
        }
    }
}
